package js.lee.jvli.config;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import js.lee.jvli.kit.XmlKit;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias("config")
public class Configuration extends AbstractNode {
    public Configuration(String name) {
        super(name);

    }

    @XStreamAsAttribute
    String               version        = "1.0";

    @XStreamAsAttribute
    String               remark         = "";

    @XStreamAlias("datasources")
    List<DataSourceNode> dataSourceList = new ArrayList<DataSourceNode>();

    @XStreamAlias("tasks")
    List<TaskNode>       taskList       = new ArrayList<TaskNode>();

    MailNode             mail;

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public List<DataSourceNode> getDataSourceList() {
        return dataSourceList;
    }

    public void setDataSourceList(List<DataSourceNode> dataSourceList) {
        this.dataSourceList = dataSourceList;
    }

    public List<TaskNode> getTaskList() {
        return taskList;
    }

    public void setTaskList(List<TaskNode> taskList) {
        this.taskList = taskList;
    }

    public MailNode getMail() {
        return mail;
    }

    public void setMail(MailNode mail) {
        this.mail = mail;
    }

    public DataSourceNode findDataSourceNode(String name) {
        for (DataSourceNode node : this.dataSourceList) {
            if (node.getName().equals(name))
                return node;
        }
        return null;
    }

    private static class SingletonHolder {
        static InputStream                 xml      = Thread.currentThread().getContextClassLoader().getResourceAsStream("task.xml");
        private static final Configuration INSTANCE = XmlKit.toBean(xml, Configuration.class);                                        ;
    }

    public static Configuration getInstance() {
        return SingletonHolder.INSTANCE;
    }
}
